package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname SelectIntTestPluginImpl
 * @Description 获取测试接口数据
 * @Date 2020/4/2 23:23
 * @Created by fpc
 */
public class SelectIntTestPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<String, Object>();
        Integer pageIndex = Integer.parseInt(Validator.getDefaultStr(String.valueOf(map.get("page")),"0"));
        Integer pageSize = Integer.parseInt(Validator.getDefaultStr(String.valueOf(map.get("rows")),"0"));
        String applyCompany = String.valueOf(map.get("dxSysIntRelation.wl_applyCompany"));
        String applySysEname = String.valueOf(map.get("dxSysIntRelation.w_applySysEname"));
        String sysEname = String.valueOf(map.get("dxSysIntRelation.w_sysEname"));
        String exchangeMode = String.valueOf(map.get("dxSysIntRelation.wl_exchangeMode"));
        String infoId = String.valueOf(map.get("dxSysIntRelation.w_infoId"));
        String fromInterfaceCode = StrUtil.doNull(String.valueOf(map.get("dxSysIntRelation.w_fromInterfaceCode")),String.valueOf(map.get("dxSysIntRelation.wl_fromInterfaceCode")));
        String fromInterfaceName = StrUtil.doNull(String.valueOf(map.get("dxSysIntRelation.wl_fromInterfaceName")),"");

        String cloum = "dsir.info_id as 'dxSysIntRelation@info_id',dsir.sys_ename as 'dxSysIntRelation@sys_ename',apply_company as 'dxSysIntRelation@apply_company',apply_sys_ename as 'dxSysIntRelation@apply_sys_ename',dsir.exchange_mode as 'dxSysIntRelation@exchange_mode',from_interface_name as 'dxSysIntRelation@from_interface_name',from_interface_code as 'dxSysIntRelation@from_interface_code',from_server_id as 'dxSysIntRelation@from_server_id',to_server_id as 'dxSysIntRelation@to_server_id',to_interface_code as 'dxSysIntRelation@to_interface_code',request_code as 'dxSysIntRelation@request_code',request_pwd as 'dxSysIntRelation@request_pwd',dsir.exchange_type as 'dxSysIntRelation@exchange_type',request_url as 'dxSysIntRelation@request_url',if_test as 'dxSysIntRelation@if_test',dir.interface_protocol as 'dxInterfaceReg@interface_protocol'";
        StringBuffer sb = new StringBuffer();
        sb.append("select ");
        sb.append(cloum);
        sb.append(" FROM dx_sys_int_relation dsir LEFT JOIN dx_interface_reg dir ON dsir.from_server_id=dir.info_id where 1=1");
        if(Validator.isNotNull(applyCompany)){
            sb.append(" and dsir.apply_company like '%"+applyCompany+"%'");
        }
        if(Validator.isNotNull(applySysEname)){
            sb.append(" and dsir.apply_sys_ename = '"+applySysEname+"'");
        }
        if(Validator.isNotNull(sysEname)){
            sb.append(" and dsir.sys_ename = '"+sysEname+"'");
        }
        if(Validator.isNotNull(exchangeMode)){
            sb.append(" and dsir.exchange_mode='"+exchangeMode+"'");
        }
        if(Validator.isNotNull(infoId)){
            sb.append(" and dsir.info_id='"+infoId+"'");
        }
        if(Validator.isNotNull(fromInterfaceCode)){
            sb.append(" and dir.interface_code like '%"+fromInterfaceCode+"%'");
        }
        if(Validator.isNotNull(fromInterfaceName)){
            sb.append(" and dir.interface_name like '%"+fromInterfaceName+"%'");
        }
        sb.append(" order by dsir.create_time desc");

        Object obj =  IusPluginUtil.getInstance().select(sb.toString(), pageIndex, pageSize);
        if(obj instanceof List){
            DataMap dataMap = (DataMap) ((List) obj).get(0);
            result.put(CustomMethodPlugin.resultMark, "操作成功");
            result.put(CustomMethodPlugin.pluginInnerReturn, dataMap);
        }else {
            result.put(CustomMethodPlugin.resultMark, "操作成功");
            result.put(CustomMethodPlugin.pluginInnerReturn, obj);
        }

        return result;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
